<template>
  <div class="orders">
    <el-card>
      <el-row :gutter="20">
        <el-col :span="12">
          <el-input
            placeholder="请输入内容"
            class="inget-with-select"
            v-model.trim="pageObj.query"
            clearable
            @clear="getSearch"
            @blur="getSearch"
            @keydown.native.enter="getSearch"
          >
            <el-button
              @click="getSearch"
              slot="append"
              icon="el-icon-search"
            ></el-button>
          </el-input>
        </el-col>
      </el-row>
      <el-table
        :data="ordersList"
        max-height="600px"
        border
        style="width: 100%; margin: 10px 0"
      >
        <el-table-column label="#" type="index"> </el-table-column>
        <el-table-column prop="order_number" label="订单编号">
        </el-table-column>
        <el-table-column prop="order_price" label="订单价格"> </el-table-column>
        <el-table-column prop="pay_status" label="是否付款"> </el-table-column>
        <el-table-column prop="is_send" label="是否发货"> </el-table-column>
        <el-table-column label="下单时间">
          <template slot-scope="scope">
            {{ scope.row.create_time | getDate }}
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button
              @click="editDialogVisible = true"
              size="mini"
              type="primary"
              icon="el-icon-edit"
            ></el-button>
            <el-button
              @click="logisticsShow(scope.row)"
              size="mini"
              type="success"
              icon="el-icon-location-outline"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <div class="block">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="pageObj.pagenum"
          :page-sizes="[5, 10, 15, 20]"
          :page-size="100"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
        >
        </el-pagination>
      </div>
    </el-card>
    <!-- 编辑 -->
    <el-dialog title="提示" :visible.sync="editDialogVisible">
      <el-form :rules="rules">
        <el-form-item
          prop="address1"
          label="省市区/县"
          :label-width="formLabelWidth"
        >
          <el-cascader
            v-model="editForm.address1"
            :options="cityData"
          ></el-cascader>
        </el-form-item>
        <el-form-item
          prop="address2"
          label="详细地址"
          :label-width="formLabelWidth"
        >
          <el-input v-model="editForm.address2"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="editDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="editDialogVisible = false"
          >确 定</el-button
        >
      </span>
    </el-dialog>
    <!-- 物流进度 -->
    <el-dialog title="物流进度" :visible.sync="logisticsDialogVisible">
      <el-timeline>
        <el-timeline-item
          v-for="(activity, index) in logisticsList"
          :key="index"
          :icon="activity.icon"
          :type="activity.type"
          :color="activity.color"
          :size="activity.size"
          :timestamp="activity.time"
        >
          {{ activity.context }}
        </el-timeline-item>
      </el-timeline>
      <span slot="footer" class="dialog-footer">
        <el-button @click="logisticsDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="logisticsDialogVisible = false"
          >确 定</el-button
        >
      </span>
    </el-dialog>
  </div>
</template>
<script>
import { getOrders, getLogistics } from "@/api/index";
import cityData from "./citydata";
export default {
  data() {
    return {
      ordersList: [],
      logisticsList: [],
      total: 0,
      pageObj: {
        query: "",
        pagenum: 1,
        pagesize: 5,
      },
      editForm: {
        address1: [],
        address2: "",
      },
      editDialogVisible: false,
      logisticsDialogVisible: false,
      formLabelWidth: "120px",
      cityData,
      rules: {
        address1: [
          { required: true, message: "请选择省市县/区", trigger: "blur" },
        ],
        address2: [
          { required: true, message: "请输入详细地址", trigger: "blur" },
        ],
      },
    };
  },
  created() {
    this.getOrdersData();
  },
  methods: {
    getSearch() {},
    handleSizeChange(val) {
      this.pageObj.pagesize = val;
      this.getOrdersData();
    },
    handleCurrentChange(val) {
      this.pageObj.pagenum = val;
      this.getOrdersData();
    },
    //物流信息
    async logisticsShow(form) {
      this.logisticsDialogVisible = true;
      let {
        data,
        meta: { message, status },
      } = await getLogistics({ id: 1106975712662 });
      if (status != 200) return this.$message.error(message);
      for (let i = 0; i < data.length; i++) {
        data[i] = {
          ...data[i],
          size: "large",
          type: "primary",
          icon: "el-icon-more",
          color: this.randomColor(),
        };
      }
      this.logisticsList = data;
    },
    // 更改地址
    editOrdersForm() {
      this.editDialogVisible = false;
    },
    // 获取数据
    async getOrdersData() {
      let {
        data: { goods, total },
        meta: { msg, status },
      } = await getOrders(this.pageObj);
      if (status != 200) return this.$message.error(msg);
      this.ordersList = goods;
      this.total = total;
    },
    randomColor() {
      let list = [
        "0",
        "1",
        "2",
        "3",
        "4",
        "5",
        "6",
        "7",
        "8",
        "9",
        "a",
        "b",
        "c",
        "d",
        "e",
        "f",
      ];
      let color = "#";
      for (let i = 0; i < 6; i++) {
        let val = Math.floor(Math.random() * list.length);
        color += list[val];
      }
      return color;
    },
  },
};
</script>